package com.lz.base.project.thread.executor;

import cn.hutool.core.date.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 *
 */
public class ExecutorLz {

    public static void main(String[] args) {
        t1();
    }

    public static void t1() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ExecutorService service = Executors.newFixedThreadPool(5);

        List<Callable<String>> tasks = new ArrayList<Callable<String>>();
        ThreadCallable qfe = new ThreadCallable("上海");
        tasks.add(qfe);
        qfe = new ThreadCallable("北京");
        tasks.add(qfe);
        qfe = new ThreadCallable("广州");
        tasks.add(qfe);
        qfe = new ThreadCallable("深圳");
        tasks.add(qfe);
        qfe = new ThreadCallable("杭州");
        tasks.add(qfe);
        try {
            List<Future<String>> futures = service.invokeAll(tasks);
            // 处理线程返回结果
            if (!futures.isEmpty()) {
                for (Future<String> future : futures) {
                    try {
                        String s = future.get();
                        System.out.println("天气：" + s);
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("CallableLz结束");
        stopWatch.stop();
        System.out.println("CallableLz总耗时：" + stopWatch.getTotalTimeMillis());
    }

}
